
package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.LoopImageController;
import com.alimama.mobile.sdk.config.LoopImageController.MMLargeImage;
import com.alimama.mobile.sdk.config.MMPromoter;
import com.alimama.mobile.sdk.config.MmuSDKFactory;
import com.xm9m.ibczy.mytest.R;

import java.util.ArrayList;
import java.util.List;

public class LoopImageDataInterfaceActivity extends BaseActivity {
    List<View> viewList;
    DemoPageAdapter adapter;
    ImageView taobaoView;
    boolean getTaobaoImageSuccess = false;
    private LoopImageController mController;
	private MmuSDK mmuSDK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loop_data_image);
        setTitle("轮播大图数据接口方式");

        ViewPager nat = (ViewPager) findViewById(R.id.loop);
        String slotId = "63882";// 注意：该广告位只做测试使用，请勿集成到发布版app中
        setupAlimama(nat, slotId);
        buildAdapterList();
        nat.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // 第二针为淘宝广告图片
                if (taobaoView != null) {
                    if (1 == arg0 && !getTaobaoImageSuccess) {
                        getTaobaoImage();
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        nat.setAdapter(adapter);

    }

    private void setupAlimama(ViewGroup nat, String slotId) {
        mmuSDK = MmuSDKFactory.getMmuSDK();
        mmuSDK.accountServiceInit(this);
        mmuSDK.init(getApplication());// 初始化SDK,该方法必须保证在集成代码前调用，可移到程序入口处调用
        mController = LoopImageController.newInstance();
        if(mController != null) {
            mController.init(slotId);
            mController.incubate();
        }
    }
    
    private void getTaobaoImage(){
        Toast.makeText(this, "正在获取图片", Toast.LENGTH_SHORT).show();
        final MMLargeImage largeImage = mController.getProduct();
        if(largeImage != null){
            List<MMPromoter> list = largeImage.getPromoters();
            if(list != null && list.size() >0){
                final MMPromoter promoter = list.get(0);
                new ImageLoadTask(promoter.getImg()) {
                    
                    @Override
                    public void onRecived(Drawable result) {
                        if(result != null){
                            Toast.makeText(LoopImageDataInterfaceActivity.this, "图片获取成功", Toast.LENGTH_SHORT).show();
                            taobaoView.setImageDrawable(result);
                            //发送展示报告，必须调用!!
                            largeImage.reportImpression(LoopImageDataInterfaceActivity.this, promoter);
                            //绑定点击事件
                            taobaoView.setOnClickListener(new OnClickListener() {
                                
                                @Override
                                public void onClick(View v) {
                                    largeImage.clickOnPromoter(LoopImageDataInterfaceActivity.this, promoter);
                                }
                            });
                            getTaobaoImageSuccess = true;
                        }else{
                            Toast.makeText(LoopImageDataInterfaceActivity.this, "图片获取失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }.execute();
            }
        }
    }
    
    public void buildAdapterList(){
        viewList = new ArrayList<View>();
        viewList.add(LayoutInflater.from(this).inflate(R.layout.loop_data_image_item, null));
        //判断广告数据是否一定返回，开发者可以自行决定是否需要预先占位
        if(mController != null && mController.hasProduct()){
            Toast.makeText(this, "广告数据一定返回", Toast.LENGTH_SHORT).show();
            taobaoView = (ImageView) LayoutInflater.from(this).inflate(R.layout.loop_data_image_item, null);
            viewList.add(taobaoView);
        }else{
            Toast.makeText(this, "广告数据不一定返回", Toast.LENGTH_SHORT).show();
        }
        viewList.add(LayoutInflater.from(this).inflate(R.layout.loop_data_image_item, null));
        viewList.add(LayoutInflater.from(this).inflate(R.layout.loop_data_image_item, null));
        adapter = new DemoPageAdapter(viewList);
    }

    class DemoPageAdapter extends PagerAdapter {
        List<View> list;

        public DemoPageAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return super.getPageTitle(position);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
    }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (!mmuSDK.accountServiceHandleResult(requestCode, resultCode, data,this)) {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}
